<?php

class photo extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('connect');
        $this->load->helper("url");
        $this->load->helper("date");
        $this->load->database();
    }

    function getphoto() {
        $a = $this->connect->getphoto2($_POST['id']);
        foreach ($a as $i) {
            $id = $i->photo_id;
            $photo = $i->photo_url;
            $time = $i->time_upload;
        }
        echo "<img class=' anh5 img-thumbnail' src='http://localhost:8080/nfa/images/photo/" . $photo . " '/>";
        echo "<br/>";


        echo "<div style='padding-top: 5%;'>";
        echo "<div style='float: left;' >";
        echo "<p ><b>Thời gian up load : <b>" . $time . "</p>";
        echo "</div>";
        echo "<div class='btn-group' style='float: right; margin-right : 5%;'>";

        echo "<button type='button' id='btn-deletephoto' data-toggle='modal' data-target='#photo" . $id . " ' class='btn btn-warning'><span class='glyphicon glyphicon-trash'></span> Xóa ảnh</button>";
        echo "</div>";
        echo "</div>";
    }

    function upload() {
        $a = $this->connect->getmaxalbumid();
        foreach ($a as $b) {
            $albumid = $b->album_id;
        }
        $datestring = "%Y/%m/%d";
        $time = time();
        $date = mdate($datestring, $time);
        if (!empty($_FILES)) {
            $tempFile = $_FILES['file']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/nfa/images/photo/';
            $targetFile = $targetPath . $_FILES['file']['name'];
            move_uploaded_file($tempFile, $targetFile);
            $data = array(
                'user_id' => $this->session->userdata('id'),
                'photo_url' => $_FILES['file']['name'],
                'time_upload' => $date,
                'album_id' => $albumid + 1
            );
            $this->db->insert('photo', $data);
        }
    }
        function addphoto(){
        
        $datestring = "%Y/%m/%d";
        $time = time();
        $date = mdate($datestring, $time);
        if (!empty($_FILES)) {
            $tempFile = $_FILES['file']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/nfa/images/photo/';
            $targetFile = $targetPath . $_FILES['file']['name'];
            move_uploaded_file($tempFile, $targetFile);
            $data = array(
                'user_id' => $this->session->userdata('id'),
                'photo_url' => $_FILES['file']['name'],
                'time_upload' => $date,
                'album_id' => $_POST['albumid']
            );
            $this->db->insert('photo', $data);
        }
    }
     

    function createalbum() {
        $a = $this->connect->getmaxalbumid();
        foreach ($a as $b) {
            $albumid = $b->album_id;
        }
        $datestring = "%Y/%m/%d";
        $time = time();
        $date = mdate($datestring, $time);
        $data = array(
            'user_id' => $this->session->userdata('id'),
            'title' => $_POST['title'],
            'time_create' => $date,
            'album_id' => $albumid + 1
        );
        $this->db->insert('album', $data);
    }

    function cancelalbum() {
        $a = $this->connect->getmaxalbumid();
        foreach ($a as $b) {
            $albumid = $b->album_id;
        }
        $this->db->where('album_id', $albumid + 1);
        $this->db->delete('photo');
    }

    function deletephoto() {
        $this->db->where('photo_id', $_POST['id']);
        $this->db->delete('photo');
    }

    function deletealbum() {
        $this->db->where('album_id', $_POST['id']);
        $this->db->delete('album');
        
         $this->db->where('album_id', $_POST['id']);
        $this->db->delete('photo');
    }

    function loadphoto() {
        $b = $this->connect->getphoto($_POST['id']);
        foreach ($b as $k) {
            $photoID = $k->photo_id;
            $photoUrl = $k->photo_url;
            
            echo "<a  class='open' onclick='zoomphoto(".$photoID.");'>";
            echo "<img class='anhnho4  img-rounded' src='http://localhost:8080/nfa/images/photo/".$photoUrl."' />";
            echo "</a>";
            
        }
    }
    
    }

?>
